import request from "@/utils/request.js"



/**
 * 修改PDF页面
 * @param {FormData} formData - 包含 filePath, pageNumber, newFile 的表单数据
 * @returns {Promise}
 */
export const updatePdfPage = (formData) => {
    return request({
        url: "/updatePdfPage",
        method: "post",
        data: formData,
        headers: {
            "Content-Type": "multipart/form-data",
        },
    })
}

/**
 * 批量更新PDF页面旋转
 * @param {Object} data - 包含文件路径和旋转信息的数据
 * @returns {Promise}
 */
export const batchUpdatePdfPages = (data) => {
    return request({
        url: "/batchUpdatePdfPages",
        method: "post",
        data: data,
        headers: {
            "Content-Type": "application/json",
        },
        timeout: 60000, // 60秒超时
    })
}


// 追加上传文件
export function appendFile(formData) {
    return request({
        url: "/upload/appendPdf",
        method: "post",
        data: formData,
        headers: {
            "Content-Type": "multipart/form-data",
        },
        timeout: 120000,
    })
}

// 获取OSS中文件的大小
export function getFileSize(filePath) {
    return request({
        url: "/getFileSize",
        method: "get",
        params: {
            filePath: filePath,
        },
    })
}


/**
 * 更新PDF页面顺序
 * @param filePath
 * @param pageOrder
 * @returns {Promise<*>}
 */
export function updatePdfPageOrder(filePath, pageOrder) {
    return request({
        url: "/updatePdfPageOrder",
        method: "post",
        params: { filePath },
        data: pageOrder,
        timeout: 120000,
    })
}

/**
 * 合并pdf
 */
export function mergeFile(pdfUrls) {
    return request({
        url: "/api/merge-pdf",
        method: "post",
        data: pdfUrls,
        responseType: "arraybuffer",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/pdf",
        },
        timeout: 120000,
    })
}


/**
 * 上传 PDF 或图片文件，并统一转为图片页
 * @param {FormData} formData - 包含文件的表单数据
 * @returns {Promise<*>}
 */
export function uploadPdfOrImage(formData) {
    return request({
        url: "/upload/file",
        method: "post",
        data: formData,
        headers: {
            "Content-Type": "multipart/form-data",
        },
        timeout: 120000,
    });
}


/**
 * 获取 PDF 转换后的所有图片页 URL
 * @param {string} filePath - PDF 文件路径
 * @returns {Promise}
 */
export function getPdfImageUrls(filePath) {
    return request({
        url: "/upload/pdfImages",
        method: "get",
        params: {
            filePath
        },
        timeout: 320000,
    });
}


/**
 * 将图片合并为PDF并下载
 */
// 修改mergeImagesToPdf接口，添加responseType: 'blob'
export function mergeImagesToPdf(filePath) {
    return request({
        url: "/upload/mergeToPdf",
        method: "get",
        params: {
            filePath
        },
        timeout: 320000,
        responseType: 'blob' // 关键：指定接收二进制流
    });
}

